package org.saratogaschools.edd.firefighter.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class AddDepartmentView extends Composite {
	
	public interface Presenter {
		void addDepartment(String name);
	}
	
	private Presenter presenter;
	
	@UiField
	TextBox nameBox;
	
	@UiField
	Button addButton;

	private static AddDepartmentViewUiBinder uiBinder = GWT
			.create(AddDepartmentViewUiBinder.class);

	interface AddDepartmentViewUiBinder extends
			UiBinder<Widget, AddDepartmentView> {
	}

	public AddDepartmentView() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	@UiHandler("addButton")
	void onAddButtonClicked(ClickEvent evt) {
		addButton.setEnabled(false);
		nameBox.setEnabled(false);
		String name = nameBox.getText();
		if (name.isEmpty() == false) {
			presenter.addDepartment(name);
		}
		else {
			addButton.setEnabled(true);
			nameBox.setEnabled(true);
			nameBox.setFocus(true);
		}
	}

}
